package org.stars.daostars.sqlmapper.loader.xml;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.daostars.core.SqlQueryReference;
import org.stars.daostars.sqlmapper.SqlMapper;
import org.stars.util.StringUtility;

import org.xml.sax.Attributes;

public abstract class IncludeTagHandler {
	private static final String ALIAS = "alias";
	private static final String QUERY = "query";
	private static final String DAO = "dao";

	protected static Log getLog()
	{
		return LogFactory.getLog(IncludeTagHandler.class);
	}
	
	/**
	 * Analizza il tag INCLUDE. Gli attributi gestiti sono:
	 * <ul>
	 * <li>dao: nome dal dao da cui recuperare la query</li>
	 * <li>query: nome della query da recuperare</li>
	 * <li>alias: nome della query all'interno del dao corrente</li>
	 * </ul>
	 * @param currentFileName
	 * @param attributes
	 * @return
	 */
	public static SqlMapper startElement(SqlMapper daoDefinition, Attributes attributes) {
		
		String sDaoName = StringUtility.nvl(attributes.getValue(DAO)).toUpperCase().trim();
		String sQueryName = StringUtility.nvl(attributes.getValue(QUERY)).toUpperCase().trim();
		String sAliasName = StringUtility.nvl(attributes.getValue(ALIAS),sQueryName).toUpperCase().trim();

		SqlQueryReference qr = new SqlQueryReference();
		qr.setDaoName(sDaoName);
		qr.setQueryName(sQueryName);
		qr.setQueryAlias(sAliasName);

		daoDefinition.setQueryReference(sQueryName, qr);
		
		return daoDefinition;
		
	}
}
